package com.example.user.mvp.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.lib_core.mvp.view.BaseActivty;
import com.example.user.R;
import com.example.user.di.component.DaggerLoginComponent;
import com.example.user.di.module.LoginModule;
import com.example.user.mvp.contract.LoginContract;
import com.example.user.mvp.presenter.LoginPresenter;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;

import static com.example.user.app.Contants.EVENT_USER_LOGIN;
import static com.example.user.app.Contants.ROUTER_LOGIN_ACT;

@Route(path = ROUTER_LOGIN_ACT)
public class LoginActivity extends BaseActivty<LoginPresenter> implements LoginContract.View, View.OnClickListener{
    private EditText mUsernameEt;
    private EditText mUserPwdEt;
    private Button mLoginBtn;


    @Override
    public int bondLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        StatusBarUtil.setTranslucent(this, 0);
        mUsernameEt = findViewById(R.id.act_login_username_et);
        mUserPwdEt = findViewById(R.id.act_login_pwd_et);
        mLoginBtn = findViewById(R.id.act_login_btn);
        mLoginBtn.setEnabled(false);

        findViewById(R.id.act_login_back_iv).setOnClickListener(this);
        findViewById(R.id.act_login_btn).setOnClickListener(this);
    }


    @Override
    public void initData() {
//如果上传登录默认写入用户名

        //如何监听输入框内容
        mUsernameEt.addTextChangedListener(new TextWatcher() {

            //之前
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            //之后
            @Override
            public void afterTextChanged(Editable s) {
                if (getUserPwd().length() > 0 && s.toString().length() > 0) {
                    mLoginBtn.setEnabled(true);
                } else {
                    mLoginBtn.setEnabled(false);
                }
            }
        });
        mUserPwdEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (getUserName().length() > 0 && s.toString().length() > 0) {
                    mLoginBtn.setEnabled(true);
                } else {
                    mLoginBtn.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModule(new LoginModule(this)).build().inject(this);
    }

    @Override
    public String getUserName() {
        return mUsernameEt.getText().toString().trim();
    }

    @Override
    public String getUserPwd() {
            return mUserPwdEt.getText().toString().trim();
    }
    @Override
    public void loginOk() {
        //广播通知页面
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().postSticky(EVENT_USER_LOGIN);
        finish();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.act_login_btn) {
            mPresenter.login();
        } else if (i == R.id.act_login_back_iv) {
            finish();
        }
    }


}
